unit QImport2Reg;

{$I VerCtrl.inc}

interface

uses SysUtils, Classes, {$IFNDEF VCL6}DsgnIntf
    {$ELSE}DesignIntf, DesignEditors{$ENDIF};

type

  TQFileNameProperty = class(TStringProperty)
  protected
    function GetDefaultExt: string; virtual; abstract;
    function GetFilter: string; virtual; abstract;
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;

  TQASCIIFileNameProperty = class(TQFileNameProperty)
  protected
    function GetDefaultExt: string; override;
    function GetFilter: string; override;
  end;

  TQDBFFileNameProperty = class(TQFileNameProperty)
  protected
    function GetDefaultExt: string; override;
    function GetFilter: string; override;
  end;

  TQXMLFileNameProperty = class(TQFileNameProperty)
  protected
    function GetDefaultExt: string; override;
    function GetFilter: string; override;
  end;

  TQXLSFileNameProperty = class(TQFileNameProperty)
  protected
    function GetDefaultExt: string; override;
    function GetFilter: string; override;
  end;

  TQTemplateFileNameProperty = class(TQFileNameProperty)
  protected
    function GetDefaultExt: string; override;
    function GetFilter: string; override;
  end;

  TQWizardFileNameProperty = class(TQFileNameProperty)
  protected
    function GetDefaultExt: string; override;
    function GetFilter: string; override;
  end;

  {$IFDEF WIN32}
  TQImportAboutProperty = class(TStringProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: string; override;
    procedure Edit; override;
  end;
  {$ENDIF}

  TQImportVersionProperty = class(TStringProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: string; override;
  end;

  TQFieldNameProperty = class(TStringProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    procedure GetValues(Proc: TGetStrProc); override;
  end;

  {$IFDEF WIN32}
  TQImportDBFEditor = class(TComponentEditor)
    function GetVerbCount: integer; override;
    function GetVerb(Index: integer): string; override;
    procedure ExecuteVerb(Index: integer); override;
  end;

  TQImportXMLEditor = class(TComponentEditor)
    function GetVerbCount: integer; override;
    function GetVerb(Index: integer): string; override;
    procedure ExecuteVerb(Index: integer); override;
  end;

  TQImportDataSetEditor = class(TComponentEditor)
    function GetVerbCount: integer; override;
    function GetVerb(Index: integer): string; override;
    procedure ExecuteVerb(Index: integer); override;
  end;

  TQImportASCIIEditor = class(TComponentEditor)
    function GetVerbCount: integer; override;
    function GetVerb(Index: integer): string; override;
    procedure ExecuteVerb(Index: integer); override;
  end;

  TQImportXLSEditor = class(TComponentEditor)
    function GetVerbCount: integer; override;
    function GetVerb(Index: integer): string; override;
    procedure ExecuteVerb(Index: integer); override;
  end;

  TQImportWizardEditor = class(TComponentEditor)
    function GetVerbCount: integer; override;
    function GetVerb(Index: integer): string; override;
    procedure ExecuteVerb(Index: integer); override;
  end;
  {$ENDIF}

procedure Register;

implementation

uses QImport2, QImport2XLS, QImport2DBF, QImport2ASCII, QImport2XML,
  QImport2Common, QImport2DataSet, DB
  {$IFDEF WIN32}
  , QImport2StrIDs, Dialogs, QImport2Wizard, fuQImport2DBFEditor,
    fuQImport2XMLEditor, fuQImport2TXTEditor, fuQImport2CSVEditor,
    fuQImport2XLSEditor, fuQImport2About, fuQImport2FormatsEditor,
    fuQImport2DataSetEditor
  {$ENDIF}
  {$IFDEF LINUX}
  , QDialogs, QImport2Consts
  {$ENDIF};

{$R *.dcr}

procedure Register;
begin
  RegisterComponents(QI_PALETTE_PAGE,
    [{$IFDEF WIN32}TQImport2Wizard,{$ENDIF}
    TQImport2XLS, TQImport2DBF, TQImport2ASCII, TQImport2XML, TQImport2DataSet]);
  RegisterPropertyEditor(TypeInfo(string), TQImport2ASCII, 'FileName',
                         TQASCIIFileNameProperty);
  RegisterPropertyEditor(TypeInfo(string), TQImport2DBF, 'FileName',
                         TQDBFFileNameProperty);
  RegisterPropertyEditor(TypeInfo(string), TQImport2XML, 'FileName',
                         TQXMLFileNameProperty);
  RegisterPropertyEditor(TypeInfo(string), TQImport2XLS, 'FileName',
                         TQXLSFileNameProperty);
  {$IFDEF WIN32}
  RegisterPropertyEditor(TypeInfo(string), TQImport2Wizard, 'FileName',
                         TQWizardFileNameProperty);
  RegisterPropertyEditor(TypeInfo(string), TQImport2, 'About',
                         TQImportAboutProperty);
  {$ENDIF}
  RegisterPropertyEditor(TypeInfo(string), TQImport2, 'Version',
                         TQImportVersionProperty);
  {$IFDEF WIN32}
  RegisterPropertyEditor(TypeInfo(string), TQImport2Wizard, 'About',
                         TQImportAboutProperty);
  RegisterPropertyEditor(TypeInfo(string), TQImport2Wizard, 'Version',
                         TQImportVersionProperty);
  RegisterPropertyEditor(TypeInfo(string), TQImport2Wizard, 'TemplateFileName',
                         TQTemplateFileNameProperty);
  {$ENDIF}
  RegisterPropertyEditor(TypeInfo(string), TQImportFieldFormat, 'FieldName',
                         TQFieldNameProperty);

  {$IFDEF WIN32}
  RegisterComponentEditor(TQImport2DBF, TQImportDBFEditor);
  RegisterComponentEditor(TQImport2XML, TQImportXMLEditor);
  RegisterComponentEditor(TQImport2DataSet, TQImportDataSetEditor);
  RegisterComponentEditor(TQImport2ASCII, TQImportASCIIEditor);
  RegisterComponentEditor(TQImport2XLS, TQImportXLSEditor);
  RegisterComponentEditor(TQImport2Wizard, TQImportWizardEditor);
  {$ENDIF}
end;

{ TQFileNameProperty }

procedure TQFileNameProperty.Edit;
var
  OD: TOpenDialog;
begin
  OD := TOpenDialog.Create(nil);
  try
    OD.DefaultExt := GetDefaultExt;
    OD.Filter := GetFilter;
    OD.FileName := GetStrValue;
    if OD.Execute then SetStrValue(OD.FileName);
  finally
    OD.Free;
  end;
end;

function TQFileNameProperty.GetAttributes: TPropertyAttributes;
begin
  Result := inherited GetAttributes + [paDialog];
end;

{ TQASCIIFileNameProperty }

function TQASCIIFileNameProperty.GetDefaultExt: string;
begin
  Result := 'txt';
end;

function TQASCIIFileNameProperty.GetFilter: string;
begin
  Result := QImportLoadStr(QIF_ASCII);
end;

{ TQDBFFileNameProperty }

function TQDBFFileNameProperty.GetDefaultExt: string;
begin
  Result := 'dbf';
end;

function TQDBFFileNameProperty.GetFilter: string;
begin
  Result := QImportLoadStr(QIF_DBF);
end;

{ TQXMLFileNameProperty }

function TQXMLFileNameProperty.GetDefaultExt: string;
begin
  Result := 'xml';
end;

function TQXMLFileNameProperty.GetFilter: string;
begin
  Result := QImportLoadStr(QIF_XML);
end;

{ TQXLSFileNameProperty }

function TQXLSFileNameProperty.GetDefaultExt: string;
begin
  Result := 'xls';
end;

function TQXLSFileNameProperty.GetFilter: string;
begin
  Result := QImportLoadStr(QIF_XLS);
end;

{ TQTemplateFileNameProperty }

function TQTemplateFileNameProperty.GetDefaultExt: string;
begin
  Result := 'imp';
end;

function TQTemplateFileNameProperty.GetFilter: string;
begin
  Result := QImportLoadStr(QIF_TEMPLATE);
end;

{ TQWizardFileNameProperty }

function TQWizardFileNameProperty.GetDefaultExt: string;
begin
  Result := EmptyStr;
end;

function TQWizardFileNameProperty.GetFilter: string;
begin
  Result := QImportLoadStr(QIF_ALL);
end;

{ TQImportAboutProperty }

{$IFDEF WIN32}
procedure TQImportAboutProperty.Edit;
begin
  ShowAboutForm;
end;

function TQImportAboutProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paReadOnly, paDialog];
end;

function TQImportAboutProperty.GetValue: string;
begin
  Result := QI_ABOUT;
end;
{$ENDIF}

{ TQImportVersionProperty }

function TQImportVersionProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paReadOnly];
end;

function TQImportVersionProperty.GetValue: string;
begin
  Result := QI_VERSION;
end;

{ TQImportDBFEditor }

{$IFDEF WIN32}
function TQImportDBFEditor.GetVerbCount: integer;
begin
  Result := 2;
end;

function TQImportDBFEditor.GetVerb(Index: integer): string;
begin
  case Index of
    0: Result := 'Map...';
    1: Result := 'Field formats...';
  end;
end;

procedure TQImportDBFEditor.ExecuteVerb(Index: integer);
begin
  case Index of
    0: RunQImportDBFEditor(Component as TQImport2DBF);
    1: if RunFormatsEditor(Component as TQImport2) then Designer.Modified;
  end;
end;
{$ENDIF}

{ TQImportXMLEditor }

{$IFDEF WIN32}
function TQImportXMLEditor.GetVerbCount: integer;
begin
  Result := 2;
end;

function TQImportXMLEditor.GetVerb(Index: integer): string;
begin
  case Index of
    0: Result := 'Map...';
    1: Result := 'Field formats...';
  end;
end;

procedure TQImportXMLEditor.ExecuteVerb(Index: integer);
begin
  case Index of
    0: RunQImportXMLEditor(Component as TQImport2XML);
    1: if RunFormatsEditor(Component as TQImport2) then Designer.Modified;
  end;
end;
{$ENDIF}

{ TQImportDataSetEditor }

{$IFDEF WIN32}
function TQImportDataSetEditor.GetVerbCount: integer;
begin
  Result := 2;
end;

function TQImportDataSetEditor.GetVerb(Index: integer): string;
begin
  case Index of
    0: Result := 'Map...';
    1: Result := 'Field formats...';
  end;
end;

procedure TQImportDataSetEditor.ExecuteVerb(Index: integer);
begin
  case Index of
    0: RunQImportDataSetEditor(Component as TQImport2DataSet);
    1: if RunFormatsEditor(Component as TQImport2) then Designer.Modified;
  end;
end;
{$ENDIF}

{ TQImportASCIIEditor }

{$IFDEF WIN32}
function TQImportASCIIEditor.GetVerbCount: integer;
begin
  Result := 2;
end;

function TQImportASCIIEditor.GetVerb(Index: integer): string;
begin
  case Index of
    0: Result := 'Map...';
    1: Result := 'Field formats...';
  end;
end;

procedure TQImportASCIIEditor.ExecuteVerb(Index: integer);
var
  NeedModify: boolean;
begin
  NeedModify := false;
  case Index of
    0: if Component is TQImport2ASCII then begin
      if (Component as TQImport2ASCII).Comma = #0
        then NeedModify := RunQImportTXTEditor(Component as TQImport2ASCII)
        else NeedModify := RunQImportCSVEditor(Component as TQImport2ASCII);
    end;
    1: NeedModify := RunFormatsEditor(Component as TQImport2);
  end;
  if NeedModify then Designer.Modified;
end;
{$ENDIF}

{ TQImportXLSEditor }

{$IFDEF WIN32}
function TQImportXLSEditor.GetVerbCount: integer;
begin
  Result := 2;
end;

function TQImportXLSEditor.GetVerb(Index: integer): string;
begin
  case Index of
    0: Result := 'Map...';
    1: Result := 'Field formats...';
  end;
end;

procedure TQImportXLSEditor.ExecuteVerb(Index: integer);
var
  NeedModify: boolean;
begin
  NeedModify := false;
  case Index of
    0: NeedModify := RunQImportXLSEditor(Component as TQImport2XLS);
    1: NeedModify := RunFormatsEditor(Component as TQImport2);
  end;
  if NeedModify then Designer.Modified;
end;
{$ENDIF}

{ TQFieldNameProperty }

function TQFieldNameProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paValueList]
end;

procedure TQFieldNameProperty.GetValues(Proc: TGetStrProc);
var
  WasInActive: Boolean;
  Import: TQImport2;
  {$IFDEF WIN32}Wizard: TQImport2Wizard;{$ENDIF}
  i: Integer;
  Component: TComponent;
  DataSet: TDataSet;
begin
  Import := nil;
  {$IFDEF WIN32}Wizard := nil;{$ENDIF}

  Component := (((GetComponent(0) as TQImportFieldFormat).Collection as
    TQImportFieldFormats).Holder);
  if Component is TQImport2 then Import := Component as TQImport2
  {$IFDEF WIN32}
  else if Component is TQImport2Wizard then Wizard := Component as TQImport2Wizard
  {$ENDIF};

  if Assigned(Import) then DataSet := Import.DataSet
  {$IFDEF WIN32}else if Assigned(Wizard) then DataSet := Wizard.DataSet{$ENDIF}
  else DataSet := nil;

  if not Assigned(DataSet) then Exit;

  WasInActive :=  not Dataset.Active;
  if WasInActive then
  try
    Dataset.Open;
  except
    Exit;
  end;
  for I := 0 to DataSet.FieldCount - 1 do begin
    Proc(DataSet.Fields[I].FieldName);
  end;
  if WasInActive then
  try
    Dataset.Close;
  except
  end;
end;

{ TQImportWizardEditor }

{$IFDEF WIN32}
procedure TQImportWizardEditor.ExecuteVerb(Index: integer);
begin
  if RunFormatsEditor(Component as TComponent{TQImport2Wizard}) then Designer.Modified;
end;

function TQImportWizardEditor.GetVerb(Index: integer): string;
begin
  Result := 'Field formats...'
end;

function TQImportWizardEditor.GetVerbCount: integer;
begin
  Result := 1;
end;
{$ENDIF}

end.
